package com.example.demo.controller;

import com.example.demo.entity.InstrumentCollection;
import com.example.demo.service.InstrumentCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/api/collections")
public class InstrumentCollectionController {

    @Autowired
    private InstrumentCollectionService instrumentCollectionService;

    // 添加收藏
    @PostMapping("/add")
    public Map<String, Object> addCollection(@RequestBody InstrumentCollection instrumentCollection) {
        boolean success = instrumentCollectionService.addCollection(instrumentCollection);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "收藏成功" : "收藏失败");
        return response;
    }

    // 删除收藏
    @DeleteMapping("/delete")
    public Map<String, Object> deleteCollection(@RequestBody InstrumentCollection instrumentCollection) {
        boolean success = instrumentCollectionService.deleteCollection(instrumentCollection);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "取消收藏成功" : "取消收藏失败");
        return response;
    }

    // 获取用户的收藏列表
    @GetMapping("/{userId}")
    public List<InstrumentCollection> getCollectionsByUserId(@PathVariable Integer userId) {
        return instrumentCollectionService.getCollectionsByUserId(userId);
    }

    // 检查是否收藏
    @GetMapping("/check")
    public Map<String, Object> isCollected(@RequestParam Integer userId, @RequestParam Integer instrumentId) {
        boolean collected = instrumentCollectionService.isCollected(userId, instrumentId);
        Map<String, Object> response = new HashMap<>();
        response.put("collected", collected);
        return response;
    }
}
